package application;

import java.io.IOException;
import java.io.InterruptedIOException;

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class SmsManager {
	private MessageConnection conn;

	public MessageConnection send(String numero, String texto)	throws IOException, InterruptedIOException {
		conn  = (MessageConnection)Connector.open ("sms://"+Trim(numero));
		TextMessage newMessage = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
		newMessage.setPayloadText(Trim(texto));
		conn.send(newMessage);
		
		return conn;
	}

	public void cleanUp() {
		if (conn!=null)
			try {
				conn.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	private String Trim(String c){
		if (c.indexOf(" ") > 0){
			return c.substring(0,c.indexOf(" "));
		}else{
			return c;			
		}
	}
}
